/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {
    var url = '/v1/manager/score/file/list.json';

    var type = $('.nav-tabs a').attr('data-id');

    var year = $('.select-year').val();

    loadList(url, type, year);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-status');

    $('.nav-tabs a').on('click', function () {
        if ($(this).attr('data-id') != type) {
            type = $(this).attr('data-id');
            if (type == 1) {
                $('#file-user').html('<div class="row user-role"><div class="col-xs-12"><div class="col-sm-2"></div><div class="col-sm-1"><a class="btn btn-sm btn-success" data-role="1" href="#">教师</a></div><div class="col-sm-1"><a class="btn btn-sm btn-default" data-role="5" href="#">学生</a></div></div></div><div class="row"><div class="col-xs-12"><table class="table table-hover data-table-user" data-height="600"></table></div></div>');
            } else {
                $('#file-department .row .col-xs-12').html('<table class="table table-hover data-table-department" data-height="600"></table>');
            }
            loadList(url, type, year);
        }
    });

    $('.select-year').on('change', function () {
        year = $(this).val();
        loadList(url, type, year);
    });

    $(document).on('click', '.user-role .btn', function () {
        $('.user-role .btn').removeClass('btn-default').removeClass('btn-success').addClass('btn-default');
        $(this).removeClass('btn-default').addClass('btn-success');
        $('.user-role').next('div').remove();
        $('.user-role').parent().append('<div class="row"><div class="col-xs-12"><table class="table table-hover data-table-user" data-height="600"></table></div></div>');
        loadList(url, type, year);
    });

    $(document).on('click', '.btn-delete', function () {
        deleteData('/v1/manager/delete/score/file', $(this).attr('data-id'));
    });

});

function loadList(url, type, year) {
    var selector = type == 1 ? '.data-table-user' : '.data-table-department';
    var role = type == 1 ? $('.user-role .btn-success').attr('data-role') : 0;
    $(selector).bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: '',
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: false,
        sortable: false,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.type = type;
            params.year = year;
            params.role = role;
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: '序号'
        }, {
            field: 'content',
            title: '积分项目'
        }, {
            field: 'score',
            title: '积分'
        },  {
            field: 'id',
            title: '操作',
            formatter: function (value) {
                var action = '<a class="btn btn-sm btn-info" href="/manager/file-edit-'+value+'.html">编辑</a>';
                action += '<a class="btn btn-sm btn-danger btn-delete" data-id="'+value+'">删除</a>';
                return action;
            }
        }]
    });
}

function refreshList() {
    $('.data-table').bootstrapTable('refresh');
}